import usart from '@ohos.Multimedia.usart';

//维护JS端缓存
export class UsartYielder{
	constructor(int_port){
		console.info("[Wang] UsartYielder::constructor");
		this.int_port = int_port;
		console.info("[Wang] UsartYielder::constructor -- setting queue");
		this.queue= [];
		console.info("[Wang] UsartYielder::constructor -- beginByteQueueWriting");
		usart.beginByteQueueWriting(int_port);
		console.info("[Wang] UsartYielder::constructor <<");
	}
	getbyte(int_bytes=1){
		var res = [];
		while(int_bytes){
			console.info("[wang] queue = "+this.queue.toString());
			while(this.queue.length ==0){
				var U8Array=new Uint8Array([]);
				while(U8Array.length ==0){
					U8Array = usart.readFromLoopingQueue(this.int_port);
				}
				const arr =Array.from(U8Array);
				this.queue=this.queue.concat(arr);
			}
			res.push(this.queue.shift());
			int_bytes=int_bytes-1;
		}
		return res;
	}
}